package com.wwwarehouse.resource_center.fragment.createres;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.res.ResBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 资源分类
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/9/15 13:52
 */
public class ResCategoryFragment extends TheParentFragment {
    CustomViewPagerInternal viewPager;
    CirclePageIndicator pageIndicator;
    ArrayList<Fragment> fragmentList;
    ElasticScrollView elasticScrollView;
    MyFragmentPagerAdapter adapter;
    int mNum = 20;

    public static final int CATEGORY = 0;  //工具
    private String businessId;

    @Override
    protected int getContentId() {
        return R.layout.fragment_tool_category;
    }

    @Override
    protected void init(View view) {
        elasticScrollView = findView(view, R.id.elastic_scroll);
        viewPager = findView(view, R.id.task_pager);
        pageIndicator = findView(view, R.id.pager_indicator);

        Bundle bundle = getArguments();
        businessId = bundle.getString("businessId");

    }


    @Override
    public void setTitle() {
        if (peekFragment() instanceof ResCategoryFragment)
            mActivity.setTitle("选择服务分类");
    }


    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        try {
            if (what == CATEGORY) {
                if (TextUtils.equals("0", commonClass.getCode())) {
                    List<ResBean.DataBean> categoryBean = JSON.parseArray(commonClass.getData().toString(), ResBean.DataBean.class);
                    fragmentList = new ArrayList<>();
                    int count = 0;
                    if (categoryBean.size() > mNum) {
                        if ((categoryBean.size() - mNum) % mNum == 0) {
                            count = (categoryBean.size() - mNum) / mNum;
                        } else {
                            count = (categoryBean.size() - mNum) / mNum + 1; // 总共显示多少张卡片
                        }
                    } else {
                        count = 0;
                    }

                    for (int i = 0; i <= count; i++) {
                        ResCategotyChildFragment categoryFragment = new ResCategotyChildFragment();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("data", (Serializable) categoryBean);
                        bundle.putInt("pos", i);
                        bundle.putInt("num", mNum);
                        categoryFragment.setArguments(bundle);
                        fragmentList.add(categoryFragment);

                    }
                    adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
                    viewPager.setAdapter(adapter);
                    pageIndicator.setViewPager(viewPager, 0);
                }
            }
        } catch (Exception e) {

        }


    }

    @Override
    protected void loadDatas() {
        requestData();


    }


    private void requestData() {
        Map<String, Object> map = new HashMap<>();
        map.put("businessId", businessId);
        httpPost(ResourceConstant.RES_CATEGORY, map, CATEGORY, false, "");
    }


    @Override
    public void onReLoad(int code) {
        requestData();
    }

}
